package service

import (
	"chat/global"
	constant "chat/internal/common"
	"chat/internal/common/request"
	"chat/internal/model"
)

func SaveMessage(msg *model.Message) error {
	return global.DB.Create(msg).Error
}

func GetPrivateMessage(mq request.MessageRequest) ([]model.Message, error) {
	var messages []model.Message
	if mq.MessageType == constant.MESSAGE_TYPE_USER {
		err := global.DB.Raw(`SELECT * FROM messages
WHERE from_user_id IN (?, ?) AND to_user_id IN (?, ?)`, mq.Uid, mq.FriendUid, mq.Uid, mq.FriendUid).Scan(&messages).Error
		return messages, err
	} else {
		// todo
		return nil, nil
	}
}

func GetAllMessByUId(uid int) ([]model.Message, error) {
	var messages []model.Message
	err := global.DB.Where("(from_user_id = ? or to_user_id = ?) and message_type = 1", uid, uid).Find(&messages).Error
	return messages, err
}

func GetGroupMessageByGroupId(list []int) ([]model.Message, error) {
	var messages []model.Message
	err := global.DB.Where("to_user_id IN ? and message_type = 2", list).Find(&messages).Error
	return messages, err
}
